package com.dc.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dc.project.entity.bo.TemplateDetailAttachmentFieldBo;
import com.dc.project.entity.map.TemplateDetailAttachmentFieldMapVo;
import com.dc.project.entity.po.TemplateDetailAttachmentField;
import com.dc.project.entity.query.TemplateDetailAttachmentFieldQuery;
import com.dc.project.entity.vo.TemplateDetailAttachmentFieldVo;
import com.dc.project.mapper.TemplateDetailAttachmentFieldMapper;
import com.dc.project.service.TemplateDetailAttachmentFieldService;
import com.own.component.common.base.service.impl.AbstractBaseService;
import com.own.exception.BusinessSimpleException;
import com.own.util.list.TreeNode;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;

/**
 * TemplateDetailAttachmentFieldServiceImpl
 *
 * @author chenxueli
 * @date 2025-03-19 13:23:08
 */
@Slf4j
@Service
public class TemplateDetailAttachmentFieldServiceImpl extends AbstractBaseService<
        TemplateDetailAttachmentField,
        TemplateDetailAttachmentFieldBo,
        TemplateDetailAttachmentFieldVo,
        TemplateDetailAttachmentFieldMapVo,
        TemplateDetailAttachmentFieldQuery,
        TemplateDetailAttachmentFieldMapper
        > implements TemplateDetailAttachmentFieldService {

    @Resource
    private TemplateDetailAttachmentFieldMapper templateDetailAttachmentFieldMapper;

    /**
     * 获取查询条件
     *
     * @param query 查询条件
     * @return 查询条件对象
     */
    @Override
    public LambdaQueryWrapper<TemplateDetailAttachmentField> lambdaQueryWrapper(TemplateDetailAttachmentFieldQuery query) {
        return super.lambdaQueryWrapper(query)
                .like(StringUtils.isNotBlank(query.getName()), TemplateDetailAttachmentField::getName, query.getName())
                .eq(query.getDetailAttachmentId() != null, TemplateDetailAttachmentField::getFkDetailAttachmentId, query.getDetailAttachmentId())
                .orderByDesc(TemplateDetailAttachmentField::getGmtCreate)
                .orderByDesc(TemplateDetailAttachmentField::getId);
    }

    /**
     * 更新数据之前
     *
     * @param templateDetailAttachmentField po对象
     * @return 处理后的po对象
     */
    @Override
    public TemplateDetailAttachmentField beforeUpdate(TemplateDetailAttachmentField templateDetailAttachmentField) {
        // 层级判断是否有重复的id
        if (templateDetailAttachmentField.getFkParentId() == null) {
            return super.beforeUpdate(templateDetailAttachmentField);
        }
        if (templateDetailAttachmentField.getFkParentId().equals(templateDetailAttachmentField.getId())) {
            throw new BusinessSimpleException("上级字段不能选择自己");
        }
        var field = templateDetailAttachmentField;
        var parentSet = new HashSet<Long>();
        parentSet.add(field.getId());
        parentSet.add(field.getFkParentId());
        while (field.getFkParentId() != null) {
            // 查询上级的值
            field = templateDetailAttachmentFieldMapper.selectById(field.getFkParentId());
            if (field == null) {
                break;
            }
            if (parentSet.contains(field.getFkParentId())) {
                throw new BusinessSimpleException("字段链上的值不能嵌套");
            }
            parentSet.add(field.getFkParentId());
        }
        return super.beforeUpdate(templateDetailAttachmentField);
    }

    /**
     * 根据附件id查询附件字段列表
     *
     * @param attachmentId 附件id
     * @return 附件字段列表
     */
    @Override
    public List<TemplateDetailAttachmentField> listByAttachmentId(Long attachmentId) {
        if (attachmentId == null) {
            return List.of();
        }
        var wrapper = lambdaQueryWrapper()
                .eq(TemplateDetailAttachmentField::getFkDetailAttachmentId, attachmentId)
                .orderByDesc(TemplateDetailAttachmentField::getGmtCreate)
                .orderByDesc(TemplateDetailAttachmentField::getId);
        return templateDetailAttachmentFieldMapper.selectList(wrapper);
    }

    /**
     * 获取字段树
     *
     * @param query 查询条件
     * @return 字段树
     */
    @Override
    public List<TreeNode<TemplateDetailAttachmentFieldMapVo>> tree(TemplateDetailAttachmentFieldQuery query) {
        var wrapper = lambdaQueryWrapper()
                .eq(TemplateDetailAttachmentField::getFkDetailAttachmentId, query.getDetailAttachmentId())
                .orderByDesc(TemplateDetailAttachmentField::getGmtCreate)
                .orderByDesc(TemplateDetailAttachmentField::getId);
        var list = templateDetailAttachmentFieldMapper.selectList(wrapper);
        var resultList = list.stream().map(TemplateDetailAttachmentFieldMapVo::new).map(TreeNode::new).toList();
        return TreeNode.build(resultList, TemplateDetailAttachmentFieldMapVo::getId, TemplateDetailAttachmentFieldMapVo::getParentId);
    }

}
